package com.dxtx.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;


/**
 * 自定义的 异常处理类 , 实现了 UncaughtExceptionHandler接口
 *
 * @author Administrator
 */
public class JrExceptionCash implements Thread.UncaughtExceptionHandler {
    // 需求是 整个应用程序 只有一个 MyCrash-Handler
    private static JrExceptionCash myCrashHandler;
    private Context context;
    //        private DoubanService service;
    private SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");

    //1.私有化构造方法
    private JrExceptionCash() {

    }

    public static synchronized JrExceptionCash getInstance() {
        if (myCrashHandler != null) {
            return myCrashHandler;
        } else {
            myCrashHandler = new JrExceptionCash();
            return myCrashHandler;
        }
    }

    public void init(Context context) {
        this.context = context;
    }


    public void uncaughtException(Thread arg0, Throwable arg1) {
        System.out.println("程序挂掉了 ");
        // 1.获取当前程序的版本号. 版本的id
        final String versioninfo = getVersionInfo();

        // 2.获取手机的硬件信息.
        final String mobileInfo = getMobileInfo();

        // 3.把错误的堆栈信息 获取出来
        final String errorinfo = getErrorInfo(arg1);

        // 4.把所有的信息 还有信息对应的时间 提交到服务器
        try {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    try {
                        File file = new File(Environment.getExternalStorageDirectory() + "/jrzhuxue/business/erro/catch.txt");
                        if (!file.getParentFile().exists()) {
                            file.getParentFile().mkdirs();
                        }
                        if (!file.exists()) {
                            file.createNewFile();
                        }
                        FileWriter fileWriter = new FileWriter(file, true);
                        fileWriter.append("\n\n\n");
                        fileWriter.append(versioninfo + mobileInfo + errorinfo);
                        fileWriter.flush();
                        fileWriter.close();
                        DLog.e("px", errorinfo);
                        //干掉当前的程序
                        android.os.Process.killProcess(android.os.Process.myPid());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            };
            AsyncTaskExecutor.getExecutor().execute(runnable);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    /**
     * 获取错误的信息
     *
     * @param arg1
     * @return
     */
    private String getErrorInfo(Throwable arg1) {
        Writer writer = new StringWriter();
        PrintWriter pw = new PrintWriter(writer);
        arg1.printStackTrace(pw);
        pw.close();
        String error = writer.toString();
        return error;
    }

    /**
     * 获取手机的硬件信息
     *
     * @return
     */
    private String getMobileInfo() {
        StringBuffer sb = new StringBuffer();
        //通过反射获取系统的硬件信息
        try {

            Field[] fields = Build.class.getDeclaredFields();
            for (Field field : fields) {
                //暴力反射 ,获取私有的信息
                field.setAccessible(true);
                String name = field.getName();
                String value = field.get(null).toString();
                sb.append(name + "=" + value);
                sb.append("\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    /**
     * 获取手机的版本信息
     *
     * @return
     */
    private String getVersionInfo() {
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);
            return info.versionName;
        } catch (Exception e) {
            e.printStackTrace();
            return "版本号未知";
        }
    }
}

